﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using Shweet.Repository;
using Shweet.Silverlight.Models;

namespace Shweet.Silverlight.ViewModels
{
    public class ShweetMainViewModel
    {
        private readonly IShweetMessage _model;
        private ObservableCollection<Shweet.Repository.Shweet> _retrievedShweets = new ObservableCollection<Shweet.Repository.Shweet>();

        public ShweetMainViewModel(IShweetMessage model)
        {
            _model = model;
            _model.ShweetLoadingComplete += _model_ShweetLoadingComplete;
        }

        public ObservableCollection<Shweet.Repository.Shweet> CurrentShweets
        {
            get { return _retrievedShweets; }
            set { _retrievedShweets = value; }
        }

        //public event EventHandler LoadComplete;

        private void _model_ShweetLoadingComplete(object sender, ShweetLoadingEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
                          {
                              _retrievedShweets.Clear();

                              foreach (Shweet.Repository.Shweet currentShweet in e.Results)
                              {
                                  _retrievedShweets.Add(currentShweet);
                              }
                          });
        }

        public void RetrieveShweetMessages()
        {
            _model.Load();
        }
    }
}